package fr.sedoo.sos.data.validation;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;


public class DoubleValidator implements CellValidator {

	private NumberFormat formatter = NumberFormat.getInstance(Locale.ENGLISH);
	private boolean mandatory;
	String lastValue;
	
	public DoubleValidator(char decimalSeparator) 
	{
		if (decimalSeparator == ',')
		{
			 formatter = NumberFormat.getInstance(Locale.FRANCE);
		}
	}
	
	public DoubleValidator(char decimalSeparator, boolean mandatory)
	{
		this(decimalSeparator);
		this.mandatory = mandatory;
	}
	
	
	public boolean isValid(String cellValue) 
	{
		if (cellValue == null)
		{
			if (mandatory == true)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			try {
				formatter.parse(cellValue);
				lastValue = cellValue;
				return true;
			}
			catch(final ParseException e) 
			{
				return false;
			}
		}
	}
	
	public String getLastValue() {
		return lastValue;
	}
}
